// 演示图像加载器 - 用于生成测试数据
class DemoImageLoader {
  static generateDemoSeries() {
    const timePoints = 10;
    const width = 256;
    const height = 256;
    const images = [];

    for (let t = 0; t < timePoints; t++) {
      // 生成时间序列图像数据
      const pixelData = new Uint16Array(width * height);
      
      for (let i = 0; i < pixelData.length; i++) {
        const x = i % width;
        const y = Math.floor(i / width);
        
        // 创建一个简单的动态模式
        const centerX = width / 2;
        const centerY = height / 2;
        const distance = Math.sqrt((x - centerX) ** 2 + (y - centerY) ** 2);
        
        // 模拟灌注模式：中心区域先达到峰值
        const timeOffset = Math.max(0, (distance / 50) - t);
        const intensity = Math.max(0, 1000 - timeOffset * 200 + Math.sin(t * 0.5) * 300);
        
        pixelData[i] = Math.max(0, Math.min(4095, intensity));
      }

      // 创建模拟的imageId
      const canvas = document.createElement('canvas');
      canvas.width = width;
      canvas.height = height;
      const ctx = canvas.getContext('2d');
      
      const imageData = ctx.createImageData(width, height);
      for (let i = 0; i < pixelData.length; i++) {
        const value = Math.floor((pixelData[i] / 4095) * 255);
        imageData.data[i * 4] = value;     // R
        imageData.data[i * 4 + 1] = value; // G
        imageData.data[i * 4 + 2] = value; // B
        imageData.data[i * 4 + 3] = 255;   // A
      }
      
      ctx.putImageData(imageData, 0, 0);
      
      images.push({
        imageId: canvas.toDataURL(),
        instanceNumber: t + 1,
        seriesInstanceUID: 'demo-series-uid',
        studyInstanceUID: 'demo-study-uid',
        patientName: '演示患者^Demo',
        studyDate: '20240101',
        modality: 'MR',
        rows: height,
        columns: width,
        pixelSpacing: [1.0, 1.0],
        sliceThickness: 5.0,
        acquisitionTime: `${String(t).padStart(2, '0')}0000`,
        dataSet: {
          elements: {},
          pixelData: pixelData
        }
      });
    }

    return {
      images: images,
      metadata: {
        patientName: '演示患者^Demo',
        studyDate: '20240101',
        modality: 'MR',
        seriesDescription: '演示灌注序列',
        institutionName: '演示医院',
        rows: height,
        columns: width,
        pixelSpacing: [1.0, 1.0],
        sliceThickness: 5.0
      },
      seriesInstanceUID: 'demo-series-uid'
    };
  }

  static generateTestPattern(width = 256, height = 256, type = 'checkerboard') {
    const pixelData = new Uint16Array(width * height);
    
    for (let i = 0; i < pixelData.length; i++) {
      const x = i % width;
      const y = Math.floor(i / width);
      
      let value = 0;
      
      switch (type) {
        case 'checkerboard':
          value = ((Math.floor(x / 32) + Math.floor(y / 32)) % 2) * 4095;
          break;
        case 'gradient':
          value = (x / width) * 4095;
          break;
        case 'circle':
          const centerX = width / 2;
          const centerY = height / 2;
          const distance = Math.sqrt((x - centerX) ** 2 + (y - centerY) ** 2);
          value = Math.max(0, 4095 - (distance / Math.min(width, height)) * 8190);
          break;
        case 'noise':
          value = Math.random() * 4095;
          break;
        default:
          value = 2048; // 中等灰度
      }
      
      pixelData[i] = Math.max(0, Math.min(4095, value));
    }
    
    return pixelData;
  }

  static createImageFromPixelData(pixelData, width, height) {
    const canvas = document.createElement('canvas');
    canvas.width = width;
    canvas.height = height;
    const ctx = canvas.getContext('2d');
    
    const imageData = ctx.createImageData(width, height);
    for (let i = 0; i < pixelData.length; i++) {
      const value = Math.floor((pixelData[i] / 4095) * 255);
      imageData.data[i * 4] = value;     // R
      imageData.data[i * 4 + 1] = value; // G
      imageData.data[i * 4 + 2] = value; // B
      imageData.data[i * 4 + 3] = 255;   // A
    }
    
    ctx.putImageData(imageData, 0, 0);
    return canvas.toDataURL();
  }
}

export { DemoImageLoader }; 